/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.log4j.Logger;
import com.crm.model.User;
import com.crm.service.IUserService;
import com.crm.web.util.Constants;
import com.javaforge.tapestry.spring.annotations.InjectSpring;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class SysUserNew extends BasePage implements PageBeginRenderListener {

    private static Logger logger = Logger.getLogger(SysUserNew.class);
    private User editUser = null;

    @Bean
    public abstract ValidationDelegate getDelegate();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectSpring("userService")
    public abstract IUserService getUserServ();

    @InjectPage("SysUser")
    public abstract SysUser getSysUserPage();

    public abstract void setUserCode(String userCode);

    public abstract String getUsername();

    public abstract void setUsername(String username);

    public abstract String getTruename();

    public abstract void setTruename(String truename);

    public abstract String getJobTitle();

    public abstract void setJobTitle(String jobTitle);

    public abstract String getSex();

    public abstract void setSex(String sex);

    public abstract String getUserGroup();

    public abstract void setUserGroup(String userGroup);

    public IPropertySelectionModel getSexModel() {
        return new StringPropertySelectionModel(Constants.SEX_SELECT);
    }

    public IPropertySelectionModel getUserGroupModel() {
        return new StringPropertySelectionModel(new String[]{User.GROUP_USERS, User.GROUP_ADMIN, User.GROUP_SUPER});
    }

    public String getSysUserCode() {
        if (editUser != null) {
            return editUser.getUserCode();
        } else {
            return getUserServ().generateUserCode();
        }
    }

    public String actionUser() {
        if (getDelegate().getHasErrors()) {
            return getPageName();
        }

        try {
            if (editUser == null) {
                User u = new User();
                u.setUserCode(getSysUserCode());
                u.setUsername(getUsername());
                u.setTruename(getTruename());
                u.setJobTitle(getJobTitle());
                u.setUserGroup(getUserGroup());
                u.setSex(getSex());
                getUserServ().createUser(u);
                logger.info(getUserSession().getUsername() + ": Created a new user!");
            } else {
                editUser.setTruename(getTruename());
                editUser.setJobTitle(getJobTitle());
                editUser.setUserGroup(getUserGroup());
                editUser.setSex(getSex());
                getUserServ().alterUser(editUser);
                logger.info(getUserSession().getUsername() + ": Alter user " + editUser.getUsername() + "!");
            }
            return "SysUser";
        } catch (Exception e) {
            String exceptionString = "System Error. " + e.getMessage();
            logger.error(getUserSession().getUsername() + ": " + exceptionString);
            e.printStackTrace();
            getDelegate().record(exceptionString, ValidationConstraint.CONSISTENCY);
            return getPageName();                        
        }
    }

    public User getEditUser() {
        return editUser;
    }

    public void setEditUser(User editUser) {
        this.editUser = editUser;
    }

    public void pageBeginRender(PageEvent event) {
        if (getUserSession().getUsername() == null || !getUserSession().getUserGroup().equals(User.GROUP_SUPER)) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        }
        if (editUser != null) {
            setUsername(editUser.getUsername());
            setTruename(editUser.getTruename());
            setJobTitle(editUser.getJobTitle());
            if (getUserGroup() == null) {
                setUserGroup(editUser.getUserGroup());
            }
            if (getSex() == null) {
                setSex(editUser.getSex());
            }
        }
    }

    public String getFieldsetName() {
        if (editUser == null) {
            return "创建用户";
        } else {
            return "修改用户";
        }
    }

    public String goback() {
        return "SysUser";
    }
}
